/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lifxue.jqda.data;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lifxue.jqda.data.bean.AppInfoBean;
import org.lifxue.jqda.util.GlobalConstant;
import org.lifxue.jqda.util.GlobalVar;
import org.lifxue.jqda.util.LnkParser;

/**
 * windows系统中“最近的文档”
 *
 * @author lifxue
 */
public class RecentData {

    private static Log logger = LogFactory.getLog(RecentData.class);

    public static List<AppInfoBean> getRecentData() {
        List<AppInfoBean> list = new ArrayList<AppInfoBean>();
        File recent = new File(GlobalVar.USER_HOME + GlobalVar.FILE_PATH_DELIMITER + GlobalConstant.RECENT);
        if (!recent.exists()) {
            return null;
        }
        if (recent.canRead()) {
            if (recent.isDirectory()) {
                String[] files = recent.list();
                for (int i = 0; i < files.length; i++) {
                    int index = files[i].lastIndexOf(".");
                    if (index == -1) {//没有“.”
                        continue;
                    }
                    String fileType = files[i].substring(index + 1);
                    if (!fileType.equals("lnk")) {//不是lnk文件
                        continue;
                    }

                    String path = GlobalVar.USER_HOME + GlobalVar.FILE_PATH_DELIMITER + GlobalConstant.RECENT + GlobalVar.FILE_PATH_DELIMITER + files[i];

                    File tempfile = new File(path);
                    String strFile = LnkParser.getLnkFile(tempfile);
                    File file = new File(strFile);
                    if(file.isDirectory()){
                        continue;
                    }
                    //logger.debug("真实文件："+strFile);

                    AppInfoBean tempApp = new AppInfoBean();
                    tempApp.setVolumeOfUse(10000);
                    tempApp.setPath(files[i] + GlobalConstant.QSFRAME_DELIMITER + path);
                    list.add(tempApp);

                }
            }
        }
        return list;
    }
}
